/**
 * 房間莊家圖標,計時器視圖控制
 * 
 * @author LuoDong
 * @email luo4521@online.net.tw
 * 2010-2-24 9:18
 */
package com.addcn.texas.view
{
	import flash.display.*;
	import flash.events.Event;	
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import com.addcn.fog.view.*;
	import com.addcn.fog.control.*;
	import com.addcn.fog.model.*;		
	import com.addcn.texas.model.*;
	import com.addcn.texas.control.*;
	import com.addcn.texas.view.components.*;	
	import com.addcn.texas.utils.Chips;
	import com.addcn.texas.utils.Coordinate;
	import com.addcn.common.utils.AppLibary;
	import com.addcn.common.utils.RemoveAllChildren;
	import com.addcn.common.event.ParamEvent;
	import com.addcn.common.utils.DestroyMemory;
	
	import caurina.transitions.Tweener;
	
	public class  GamePropsView  extends View 
	{		
		private var _container : Object;
		private var _app : AppLibary;
		private var _components : Object;		
		private var _bet_class : Class;
		private var _game_clock : GameClock;
		private var _banker_pos : Array;
		private var _clock_pos : Array;			
		private var _call_sit : int;
		
		private var _time_over : int; //玩家超时次数
		
		public function GamePropsView(r_target : Object) : void
		{
			this._container = r_target;
			_initialize();
		}
		
		private function _initialize() : void
		{
			//构建容器
			var _sprite = new Sprite;
			this._container.addChild(_sprite);
			this._container = _sprite;
			
			_banker_pos = Coordinate.getInstance().BankerCoord();							
			_clock_pos = Coordinate.getInstance().SitCoord();				
			this._app = AppLibary.getInstance();	
			this._bet_class = _app.getClass("com.addcn.texas.view.components.BetPanel");
			this._components = [];	
			
			this._game_clock = GameClock.getInstance();
			this._game_clock.initialize();
			
			//this._game_clock.addEventListener(GameClock.TIME_EVENT, _removeClock);
		}
		
		override public function notificationsList():void 
		{
			notifications = [
							RoomUserModel.USER_STANDUP,
							GameInfoModel.GAME_START,
							GameInfoModel.SET_BANKER,							
							GameInfoModel.NEXT_PLAYER_CALL,																								
							GameInfoModel.GAME_OVER,
							GameInfoModel.LOSE_PLAYER,
							
							];
		}
		
		override public function onDataChanged(r_notification:String, r_data:Object = null):void {
			switch (r_notification) {
				case RoomUserModel.USER_STANDUP:
					_time_over = 0;
					if (this._call_sit == r_data["sit"]) {
						_removeClock();
						var _obj = this._components["BetPanel"];
						if(_obj){
							_obj.parent.removeChild(_obj);
							this._components["BetPanel"] = null;
						}
					}
					break;
				case GameInfoModel.GAME_START:
					
					break;
				case GameInfoModel.SET_BANKER:  //設置莊家
					_setBanker(r_data["sit"]);
					break;				
				case GameInfoModel.NEXT_PLAYER_CALL:  						
					_setClock(r_data["sit"]); //設置計時器							
					_showBetPanel(r_data);   //顯示下注操作面板
					break;		
				case GameInfoModel.GAME_OVER:  //游戲結束,分配池底
					_removeClock();
					//DestroyMemory.clear();
					break;
				case GameInfoModel.LOSE_PLAYER:					
					_removeClock();
					//DestroyMemory.clear();
					break;			
			}
		}		
		
		/**============================================================
		 * 設置莊家
		 * @param	r_sit int 座位號
		 * ============================================================
		 */
		private function _setBanker(r_sit : int) : void
		{		
			var _banker : Object = this._components["banker"];	
			if (_banker != null) {		
				if (r_sit == 0) {
					this._container.removeChild(_banker);
					this._components["banker"] = null;
					return;
				}
				var _pos : Object = this._banker_pos[r_sit];
				var _option : Object = { };
				_option.x = _pos.x;
				_option.y = _pos.y;
				_option.time = 1;
				_option.transition = "easeOutSine";
				_option.onComplete = _tweenrCompletet;						
				Tweener.addTween(_banker, _option );
			}else {
				_addBanker(r_sit);
			}
		}
		
		//添加莊家圖標到顯示列表
		private function _addBanker(r_sit : int) : void
		{
			if (r_sit < 1 || r_sit >9) return;
			var _class = this._app.getClass("Banker");
			if (!_class) return;
			var _banker = new _class;
			_banker.x = _banker_pos[r_sit]['x'];
			_banker.y = _banker_pos[r_sit]['y'];
			this._container.addChild(_banker);
			this._components["banker"] = _banker;
		}
		
		private function _tweenrCompletet() : void
		{
			Tweener.removeTweens(this._components["banker"], "x", "y");
		}
		
		/**============================================================
		 * 設置計時器
		 * @param	r_sit int 座位號
		 * ============================================================
		 */
		private function _setClock(r_sit : int) : void
		{	
			_removeClock();
			var _loc : Object = this._clock_pos[r_sit];
			_game_clock.x = _loc.x;
			_game_clock.y = _loc.y;
			this._container.addChild(this._game_clock);			
			_game_clock.timeStart();	
			_game_clock.addEventListener(GameClock.TIME_EVENT, _timerOver);
			this._call_sit = r_sit;
		}
		
		//清除計時器
		private function _removeClock( ) : void
		{			
			_game_clock.timeStop();
			if (this._game_clock.parent) {				
				this._game_clock.parent.removeChild(_game_clock);
			}				
		}
		
		private function _timerOver(evt : Event) : void
		{
			_removeBetPanel();
			_betHandler(new ParamEvent(BetPanel.BET_EVENT, { chip:0, way:2 } ));
			_time_over++;		
			//玩家超时两次，自动站起
			if (_time_over > 2) {
				var _model : RoomUserModel = ModelLocator.getInstance().getModel(RoomUserModel);
				_model.standUp();
				_time_over = 0;
			}
		}
		
		
		/**============================================================
		 * 下注操作面板
		 * @param	r_data Object
		 * ============================================================
		 */
		private function _showBetPanel(r_data : Object) : void
		{							
			if("show" in r_data){
				var _obj  = this._container.getChildByName("BetPanel");
				if (!_obj) {
					_obj = new this._bet_class;
					_obj.name = "BetPanel";
					this._container.addChild(_obj);
					_obj.x = 273;
					_obj.y = 528;
					_obj.addEventListener(BetPanel.BET_EVENT, _betHandler);						
				}			
				_obj.setLimit(r_data);				
				
			}
		}
				
		private function _betHandler(evt : ParamEvent) : void
		{		
			_removeBetPanel();
			_removeClock();
			var _params : Object = evt.params;			
			var _model : GameInfoModel = ModelLocator.getInstance().getModel(GameInfoModel);
			_model.bet(_params);										
		}
		
		private function _removeBetPanel() : void
		{
			var _obj  = this._container.getChildByName("BetPanel");
			if (_obj) {
				_obj.parent.removeChild(_obj);
			}
		}
			
					
		
		
		override public function destroy():void 
		{
			this._game_clock.destroy();				
			RemoveAllChildren.remove(this._container as DisplayObject);			
		}
		
	}
	
}
